package com.feather.common.db.impl;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * @author flogyin
 */
public class JndiDataSource {
	public String	factory;
	public String	url;
	public String	jndi;

	public DataSource getDataSource() throws NamingException {
		Properties env = new Properties();
		if (this.factory != null && this.url != null) {
			env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
			env.put(Context.PROVIDER_URL, url);
		}
		Context initCtx = new InitialContext(env);
		return (DataSource) initCtx.lookup(this.jndi);
	}

	@Override
	public String toString() {
		return this.getClass().getName() + "{factory:" + this.factory + "url:" + this.url + "jndi:" + this.jndi + "}";
	}
}